package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.filecenter.annotations.NeedRole;
import com.bwda.dsrs.filecenter.aspect.LogTypeEnum;
import com.bwda.dsrs.filecenter.aspect.Loggable;
import com.bwda.dsrs.filecenter.domain.BusinessConstants;
import com.bwda.dsrs.filecenter.domain.form.terminalblacklist.TerminalBlackListCreateForm;
import com.bwda.dsrs.filecenter.domain.form.terminalblacklist.TerminalBlackListQueryForm;
import com.bwda.dsrs.filecenter.domain.form.terminalblacklist.TerminalBlackListUpdateForm;
import com.bwda.dsrs.filecenter.domain.po.terminalblacklist.TerminalBlackListPo;
import com.bwda.dsrs.filecenter.domain.condition.terminalblacklist.TerminalBlackListCondition;
import com.bwda.dsrs.filecenter.domain.vo.terminalblacklist.TerminalBlackListVo;
import com.bwda.dsrs.filecenter.service.TerminalBlackListService;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import com.bwda.dsrs.base.util.CopyUtil;
import com.bwda.dsrs.filecenter.util.IpTest;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 终端黑名单表
 *
 * @author wangju
 * @date 2019/8/19
 */
@RestController
@RequestMapping("/b/terminalblacklist")
@Api(value = "/b/terminalblacklist", tags = "终端黑名单表")
public class TerminalBlackListController extends BaseController {

	@Autowired
	private TerminalBlackListService terminalBlackListService;

	@NeedRole("sysadminrole")
	@ApiOperation(value = "查询终端黑名单表",notes = "查询终端黑名单表",httpMethod = "GET")
	@Loggable(descp = "查询终端黑名单表",type = LogTypeEnum.QUERY,include = "")
	@RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<TerminalBlackListVo> query(@ApiParam(value = "ID", required = true)@RequestParam BigDecimal id) throws BusinessServiceException {
		TerminalBlackListPo po = terminalBlackListService.queryWithValid(id);
		TerminalBlackListVo vo = CopyUtil.transfer(po, TerminalBlackListVo.class);
		return getSuccessResult(vo);
	}

	@NeedRole("sysadminrole")
	@ApiOperation(value = "查询终端黑名单表数量",notes = "查询终端黑名单表数量",httpMethod = "GET")
	@Loggable(descp = "查询终端黑名单表数量",type = LogTypeEnum.QUERY,include = "")
	@RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<Integer> queryCount(@ModelAttribute@Valid TerminalBlackListQueryForm form) throws BusinessServiceException {
		TerminalBlackListCondition condition = this.getConditionByQueryForm(form);
		int count = terminalBlackListService.queryCount(condition);
		return getSuccessResult(count);
	}
	@NeedRole("sysadminrole")
	@ApiOperation(value = "查询终端黑名单表列表",notes = "查询终端黑名单表列表",httpMethod = "GET")
	@Loggable(descp = "查询终端黑名单表列表",type = LogTypeEnum.QUERY,include = "")
	@RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<TerminalBlackListVo>> queryList(@ModelAttribute@Valid TerminalBlackListQueryForm form) throws BusinessServiceException {
		TerminalBlackListCondition condition = this.getConditionByQueryForm(form);
		List<TerminalBlackListPo> poList = terminalBlackListService.queryList(condition);
		List<TerminalBlackListVo> voList = CopyUtil.transfer(poList, TerminalBlackListVo.class);
		return getSuccessResult(getListResponse(voList));
	}
	@NeedRole("sysadminrole")
	@ApiOperation(value = "查询终端黑名单表列表(带分页)",notes = "查询终端黑名单表列表(带分页)",httpMethod = "GET")
	@Loggable(descp = "查询终端黑名单表列表(带分页)",type = LogTypeEnum.QUERY,include = "")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<TerminalBlackListVo>> queryPageList(@ModelAttribute@Valid TerminalBlackListQueryForm form) throws BusinessServiceException {
		if(form.getMacAddr()!=null){
			form.setMacAddr(form.getMacAddr().toUpperCase());
		}
		TerminalBlackListCondition condition = this.getConditionByQueryForm(form);
		List<TerminalBlackListVo> voList = new ArrayList<>();
		condition.setDelFlag(BusinessConstants.NumberConstant.ZERO);
		int count = terminalBlackListService.queryCount(condition);
		if (count > 0) {
			List<TerminalBlackListPo> poList = terminalBlackListService.queryList(condition);
			voList = CopyUtil.transfer(poList, TerminalBlackListVo.class);
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}
	@NeedRole("sysadminrole")
	@ApiOperation(value = "新增终端黑名单表",notes = "新增终端黑名单表",httpMethod = "POST")
	@Loggable(descp = "新增终端黑名单表",type = LogTypeEnum.CREATE,include = "")
	@RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<TerminalBlackListVo> add(@ModelAttribute@Valid TerminalBlackListCreateForm form) throws BusinessServiceException {
		TerminalBlackListPo po = CopyUtil.transfer(form, TerminalBlackListPo.class);
		IpTest ipTest = new IpTest();
		if (!ipTest.checkSameSegment(form.getIpEnd(),form.getIpStart())){
			return getFailResult("ip不在同一网段，请重新输入");
		}
		po.setCreateUserCode(getUserCode());
		terminalBlackListService.insert(po);
		TerminalBlackListVo vo = CopyUtil.transfer(po, TerminalBlackListVo.class);
		return getSuccessResult(vo);
	}
	@NeedRole("sysadminrole")
	@ApiOperation(value = "修改终端黑名单表",notes = "修改终端黑名单表",httpMethod = "POST")
	@Loggable(descp = "修改终端黑名单表",type = LogTypeEnum.UPDATE,include = "")
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity update(@ModelAttribute@Valid TerminalBlackListUpdateForm form) throws BusinessServiceException {
		TerminalBlackListPo po = CopyUtil.transfer(form, TerminalBlackListPo.class);
		IpTest ipTest = new IpTest();
		if (!ipTest.checkSameSegment(form.getIpEnd(),form.getIpStart())){
			return getFailResult("ip不在同一网段，请重新输入");
		}
		po.setUpdateUserCode(getUserCode());
		po.setUpdateTime(new Date());
		terminalBlackListService.update(po);
		return getSuccessResult();
	}
	@NeedRole("sysadminrole")
	@ApiOperation(value = "删除终端黑名单表",notes = "删除终端黑名单表",httpMethod = "POST")
	@Loggable(descp = "删除终端黑名单表",type = LogTypeEnum.DELETE,include = "")
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity delete(@ApiParam(value = "ID", required = true)@RequestParam List<Integer> id) throws BusinessServiceException {
		if (id == null || id.size() == 0) {
			return getFailResult("参数错误");
		}
		terminalBlackListService.batchDelete(id);
		return getSuccessResult();
	}

	/**
	 * TerminalBlackListQueryForm转换为TerminalBlackListCondition
	 *
	 * @param form
	 * @return
	 */
	private TerminalBlackListCondition getConditionByQueryForm(TerminalBlackListQueryForm form) {
		TerminalBlackListCondition condition = CopyUtil.transfer(form, TerminalBlackListCondition.class);
		return condition;
	}

}